/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package domain;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.PrintWriter;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.Formatter;
import java.util.List;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import mapping.ClientMapper;
import network.Network;

/**
 *
 * @author Jasper
 */
public class OverviewController {
    private  ClientMapper clientMapper ;
    private ServerSocket server;
    private ArrayList<Client> clients = new ArrayList<Client>();
    private ArrayList<Client> tekenaars = new ArrayList<Client>();
    private ArrayList<Client> raders = new ArrayList<Client>();
    public List<Client> getTekenaars(){
        return tekenaars;
    }
    
    public List<Client> getRaders(){
        return raders;
    }
    
    public void getClients(Socket link) {
        try {
            PrintWriter output = new PrintWriter(link.getOutputStream(), true);
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            String type;
            while((type = input.readLine()) != null){
                clients.addAll(clientMapper.getClients(type));
                OutputStream outputStream = link.getOutputStream();
                ObjectOutputStream stream = new ObjectOutputStream(outputStream);
                stream.writeObject(clients);
                System.out.println("clients verzenden...");
                output.println(stream);
                output.flush();
                System.out.println("verzonden");
                output.close();  
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }         
    }
    
    public void startGame(Socket link){
        try {
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            String naam;
            naam = input.readLine();
            Client client = clientMapper.getClient(naam);
            Spel spel = new Spel();
            System.out.println("Er wordt een nieuw spel aangemaakt");
            spel.setRaders(clientMapper.getRaders());
            spel.setTekenaars(clientMapper.getTekenaars());
            if(client.getSoort().equals("rader")){
                spel.addRader(client);
            }else{
                spel.addTekenaar(client);
            }
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void addRole(Socket link){
        try {
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            String naam, role;
            naam = input.readLine();
            role = input.readLine();
            System.out.printf("%s verandert in %s \n", naam,role);
            Client client = clientMapper.getClient(naam);
            clientMapper.removeVeranderenSoort(client, role);
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public OverviewController(ClientMapper clientmapper){
        try{
            server = new ServerSocket(Network.PORT);
            this.clientMapper = clientMapper;
            run();
        }catch(IOException e){
            e.printStackTrace();
            System.exit(1);
        }
    }

    private void run() {
        Socket link = null;
        try{
            link = server.accept();
            BufferedReader input = new BufferedReader(new InputStreamReader(link.getInputStream()));
            String opdracht = input.readLine();
            switch(opdracht){
                case "getClients": getClients(link);break;
                case "startGame": startGame(link);break;
                case "addRole": addRole(link);break;
            }
        }catch(IOException e){
            e.printStackTrace();
        }
        finally
        {
            try{
                link.setReuseAddress(true); 
                server.close();
            }catch(IOException e){
                System.exit(1);
            }
        }
    }
}
